package allod.som.input

import allod.som.Lattice.Pattern
import scala.xml._

/**
 * Created by IntelliJ IDEA.
 * User: allod
 * Date: 3 січ 2010
 * Time: 20:14:24
 */

class XMLInputSource(node: NodeSeq) extends InputSource{
  val inputDimension = (node \ "dimension").text.toInt

  lazy val elements: Seq[Pattern] = elementsFromXML(node \ "vectors")

  lazy val labels = labelsFromXML(node \ "vectors")

  require(elements.size == labels.size)

  def toXML(): NodeSeq = node

  private def elementsFromXML(nodeSeq: NodeSeq): Seq[Pattern] = {
    val vectors = nodeSeq \\ "vector"
    for{vector <- vectors} yield vectorFromXML(vector)
  }

  private def vectorFromXML(node: Node): Pattern = {
    val items = node \\ "item"
    val seq = for{item <- items} yield item.text.toDouble
    seq.toList
  }

  private def labelsFromXML(nodeSeq: NodeSeq): Seq[String] = {
    val labelSeq = nodeSeq \\ "vector" \\ "label"
    for{label <- labelSeq} yield label.text.toString
  }

}